package cn.explink.dop.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.explink.dop.entity.vo.RestaurantVO;
import cn.explink.dop.entity.vo.StorefrontVO;
import cn.explink.dop.service.RestaurantService;
import cn.explink.dop.service.StorefrontService;
import cn.explink.eap.core.common.model.json.AjaxJson;
import cn.explink.eap.web.cgform.controller.autolist.CgAutoListController;

/**
 * 店面信息控制器
 *
 * @author firefly 2015-2-27 10:39
 *
 */
@Controller
@RequestMapping("/storefrontController")
public class StorefrontController extends CgAutoListController {

	@Autowired
	private StorefrontService storefrontService;

	@Autowired
	private RestaurantService restaurantService;

	/**
	 * 基本信息
	 *
	 * @param request
	 *            请求
	 * @return 基本信息页面
	 */
	@RequestMapping(params = "index")
	public ModelAndView storefrontIndex(HttpServletRequest request) {
		ModelAndView view = new ModelAndView("dop/storefront/storefront");
		return view;
	}

	@RequestMapping(params = "create")
	@ResponseBody
	public AjaxJson saveBaseInfo(String stoName, String stoAddress, String stoCoordinate, String stoStartTime, String stoEndTime, String restaurantId, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		try {
			RestaurantVO restaurantVO = this.restaurantService.getById(restaurantId);

			StorefrontVO sto = new StorefrontVO();
			sto.setStoStatus(1);
			sto.setStoName(stoName);
			sto.setStoAddress(stoAddress);
			sto.setStoCoordinate(stoCoordinate);
			sto.setStoStarttime(stoStartTime);
			sto.setStoEndtime(stoEndTime);
			sto.setRestaurant(restaurantVO);

			String id = this.storefrontService.add(sto);
			if (null != id) {
				j.setSuccess(true);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return j;
	}

	@RequestMapping(params = "modify")
	@ResponseBody
	public AjaxJson updateData(String id, String stoName, String stoAddress, String stoCoordinate, String stoStartTime, String stoEndTime, String restaurantId, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		try {
			RestaurantVO restaurantVO = this.restaurantService.getById(restaurantId);

			StorefrontVO sto = new StorefrontVO();
			sto.setId(id);
			sto.setStoName(stoName);
			sto.setStoAddress(stoAddress);
			sto.setStoCoordinate(stoCoordinate);
			sto.setStoStarttime(stoStartTime);
			sto.setStoEndtime(stoEndTime);
			sto.setRestaurant(restaurantVO);

			this.storefrontService.modify(sto);
			j.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return j;
	}

	/**
	 * 获取数据
	 *
	 * @param pageNum
	 *            当前页
	 * @param pageSize
	 *            当前页条数
	 * @param request
	 *            请求
	 * @return 结果集
	 */
	@RequestMapping(params = "loadData")
	@ResponseBody
	@SuppressWarnings("unchecked")
	public List<StorefrontVO> loadData(int pageNum, int pageSize, HttpServletRequest request) {
		try {
			Map<String, Object> map = this.storefrontService.getListDataByPage(new StorefrontVO(), pageNum, pageSize);
			return (List<StorefrontVO>) map.get("list");
		} catch (Exception e) {
			e.printStackTrace();
			return new ArrayList<StorefrontVO>();
		}
	}

	/**
	 *
	 * @param request
	 * @return
	 */
	@RequestMapping(params = "findData")
	@ResponseBody
	public List<StorefrontVO> findData(HttpServletRequest request) {
		try {
			StorefrontVO vo = new StorefrontVO();
			vo.setStoStatus(1);
			List<StorefrontVO> list = this.storefrontService.getListData(vo);
			return list;
		} catch (Exception e) {
			e.printStackTrace();
			return new ArrayList<StorefrontVO>();
		}
	}

	/**
	 *
	 * @param request
	 * @return
	 */
	@RequestMapping(params = "getById")
	@ResponseBody
	public StorefrontVO getById(String id, HttpServletRequest request) {
		try {
			StorefrontVO storefrontVO = this.storefrontService.queryById(id);
			return storefrontVO;
		} catch (Exception e) {
			e.printStackTrace();
			return new StorefrontVO();
		}
	}

	/**
	 * 获取查询数据
	 *
	 * @param pageNum
	 *            当前页
	 * @param pageSize
	 *            当前页条数
	 * @param request
	 *            请求
	 * @return 结果集
	 */
	@RequestMapping(params = "queryList")
	@ResponseBody
	public Map<String, Object> queryData(StorefrontVO sto, String stoName, String stoAddress, String restaurantId, int pageNum, int pageSize, HttpServletRequest request) {
		try {
			if (StringUtils.isNotBlank(restaurantId)) {
				RestaurantVO restaurantVO = new RestaurantVO();
				restaurantVO.setId(restaurantId);
				sto.setRestaurant(restaurantVO);
			}
			return this.storefrontService.getListDataByPage(sto, pageNum, pageSize);
		} catch (Exception e) {
			e.printStackTrace();
			return new HashMap<String, Object>();
		}
	}

	/**
	 * 删除数据
	 *
	 * @param rowList
	 *            数据字符串
	 * @param request
	 *            求情
	 * @return 删除状态
	 */
	@RequestMapping(params = "delete")
	@ResponseBody
	@SuppressWarnings("unchecked")
	public AjaxJson deleteRows(String rowList, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		try {
			JSONArray json = JSONArray.fromObject(rowList);
			List<StorefrontVO> list = JSONArray.toList(json, StorefrontVO.class);
			if (list.size() == this.storefrontService.deleteData(list)) {
				j.setSuccess(true);
			} else {
				j.setSuccess(false);
			}
		} catch (Exception e) {
			e.printStackTrace();
			j.setSuccess(false);
		}
		return j;
	}

	/**
	 *
	 * @Title: openSto
	 * @Description: 启用店面
	 * @author 孟凡岭
	 * @param @param idsArr
	 * @param @param request
	 * @param @return
	 * @return AjaxJson 返回类型
	 * @throws
	 */
	@RequestMapping(params = "opensto")
	@ResponseBody
	public AjaxJson openSto(@RequestParam(value = "idsArr[]", required = false) String[] idsArr, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		try {
			this.storefrontService.openSto(idsArr);
			j.setSuccess(true);
			return j;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			j.setSuccess(false);
			return j;
		}
	}

	/**
	 *
	 * @Title: stopSto
	 * @Description: 停用店面
	 * @author 孟凡岭
	 * @param @param idsArr
	 * @param @param request
	 * @param @return
	 * @return AjaxJson 返回类型
	 * @throws
	 */
	@RequestMapping(params = "stopsto")
	@ResponseBody
	public AjaxJson stopSto(@RequestParam(value = "idsArr[]", required = false) String[] idsArr, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		try {
			boolean flag = this.storefrontService.stopSto(idsArr);
			j.setSuccess(flag);
			return j;
		} catch (Exception e) {
			// TODO: handle exception
			j.setSuccess(false);
			return j;
		}
	}

}
